package com.psj.service.impl;

import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import com.psj.bean.*;
import com.psj.mapper.MainMapper;
import com.psj.service.MainService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;
import java.util.List;

@Service
@Transactional
public class MainServiceImpl implements MainService {

    @Autowired
    MainMapper mainMapper;

    @Override
    public PageInfo<Main> list(Main main, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<Main> list = mainMapper.list(main);
        return new PageInfo<Main>(list);
    }

    @Override
    public List<Stadium> listStadium() {
        return mainMapper.listStadium();
    }

    @Override
    public List<Type> listType() {
        return mainMapper.listType();
    }

    @Override
    public List<Place> listPlace() {
        return mainMapper.listPlace();
    }

    @Override
    public int add(Main main) {
        //先添加子表
        Date created = new Date();
        main.setCreated(created);
        int i = mainMapper.addMain(main);
        //再添加子表
        List<Info> infoList = main.getInfoList();
        if(null!=infoList&&infoList.size()>0){
            for (Info info : infoList) {
                if( ("".equals(info.getStartTime())||null==info.getStartTime()) ||
                        ("".equals(info.getEndTime())||null==info.getEndTime()) ||
                        ("".equals(info.getPrice())||null==info.getPrice())
                ){
                    continue;
                }
                info.setPid(main.getId());
                mainMapper.addInfo(info);
            }
        }
        return i;
    }

    @Override
    public List<Main> getCallTj() {
        return mainMapper.getCallTj();
    }

}
